package com.dk.realm;

import com.dk.pojo.User;
import com.dk.service.LoginService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

public class LoginRealm extends AuthorizingRealm {

    @Autowired
    private LoginService loginService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //向下转型
        UsernamePasswordToken upToken = (UsernamePasswordToken)authenticationToken;
        //从数据库中获取数据
        User user = loginService.selectByName(upToken.getUsername());
        //判断
        if (user == null) {
            return null;
        }
        //校验密码
        SimpleAuthenticationInfo sim = new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes(user.getSalt()),"loginRealm");
        return sim;
    }
}